<?php

namespace app\platform\controller;

use app\BaseController;

use app\common\exception\BaseException;

use app\common\helper\open\OpenPlatformMiniappHelper;

use think\db\concern\WhereQuery;

use think\facade\Cache;

use think\facade\Db;



// 推送消息

class MemberNotice extends BaseController{



    // 商家通知

    /**

     * 会员注册通知

     * parameter -- $member_id(Int) ----- 用户ID

     * parameter -- $nick_name(String) -- 用户名称

     * parameter -- $source(String) ----- 注册来源

     */

    public function member_register_notice($member_id,$nick_name,$source){



        $AppidData = $this->member_info($member_id);

        if($AppidData['member_register_notice']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['member_register_notice'];

            // 构建请求

            /*

             * {{first.DATA}}->你的店铺有新会员注册

             * 注册用户：{{keyword1.DATA}}=显示用户名称

             * 注册时间：{{keyword2.DATA}}=注册时间

             * 注册来源：{{keyword3.DATA}}=注册来源

             * {{remark.DATA}}->更多信息请到后台查看

             * */

            $message_data = [

                $nick_name,

                date("Y-m-d H:i:s",time()),

                $source

            ];

            $tem = [];

            $tem['first'] = '你的店铺有新会员注册';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '更多信息请到后台查看';

            $this->staff_id($AppidData['shop_id'],$template_id,$tem,$url);

        }



    }



    /**

     * 会员在线预约

     * parameter -- $member_id(Int) -------- 用户ID

     * parameter -- $nick_name(String) ----- 用户名称

     * parameter -- $mobile(Int) ----------- 用户手机号

     * parameter -- $abbreviation(String) -- 课程名称

     * parameter -- $day(String) ----------- 日期

     * parameter -- $start_time(String) ---- 时间

     */

    public function member_appointment($member_id,$nick_name,$mobile,$abbreviation,$day,$start_time){



        $AppidData = $this->member_info($member_id);

        if($AppidData['member_appointment']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['member_appointment'];



            // 构建请求

            /*

                {{first.DATA}}=你的店铺有新的预约

                客户姓名：{{keyword1.DATA}}=显示用户名称

                客户手机：{{keyword2.DATA}}=显示用户手机号

                预约时间：{{keyword3.DATA}}=显示预约的上课时间

                预约内容：{{keyword4.DATA}}=显示预约的课程名称

                {{remark.DATA}}=请及时处理

             * */

            $message_data = [

                $nick_name,

                $mobile,

                $day.' '.$start_time,

                $abbreviation

            ];

            $tem = [];

            $tem['first'] = '你的店铺有新的预约';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '请及时处理';

            return $this->staff_id($AppidData['shop_id'],$template_id,$tem,$url);

        }

    }



    /**

     * 会员取消预约

     * parameter -- $member_id(Int) ---------------- 用户ID

     * parameter -- $membership_card_id(Int) ------- 开通ID

     * parameter -- $need_pay(Int) ----------------- 订单金额

     * parameter -- $membership_card_name(String) -- 课程名称

     */

    public function member_cancels_appointment($member_id,$project_name,$day,$start_time,$type){



        $AppidData = $this->member_info($member_id);

        if($AppidData['member_cancels_appointment'] && $AppidData['mp_openid']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['member_cancels_appointment'];

            // 构建请求

            /*

                {{first.DATA}}=你的店铺有会员取消了预约

                预约项目：{{keyword1.DATA}}=显示预约课程名称

                预约时间：{{keyword2.DATA}}=显示预约上课的时间

                取消原因：{{keyword3.DATA}}=顾客自主取消

                {{remark.DATA}}=更多信息请到后台查看

             * */

            $message_data = [

                $project_name,

                $day.' '.$start_time,

                $type

            ];

            $tem = [];

            $tem['first'] = '你的店铺有会员取消了预约';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '更多信息请到后台查看';

            return $this->staff_id($AppidData['shop_id'],$template_id,$tem,$url);

        }



    }



    /**

     * 会员支付订单

     * parameter -- $member_id(Int) ---------------- 用户ID

     * parameter -- $membership_card_id(Int) ------- 开通ID

     * parameter -- $need_pay(Int) ----------------- 订单金额

     * parameter -- $membership_card_name(String) -- 课程名称

     */

    public function member_payment_order($member_id,$membership_card_id,$need_pay,$membership_card_name){



        $AppidData = $this->member_info($member_id);

        if($AppidData['member_payment_order']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['member_payment_order'];

            // 构建请求

            /*

                {{first.DATA}}=你的店铺有会员在线支付了一笔新的订单

                用户名：{{keyword1.DATA}}=显示支付的用户名称

                订单号：{{keyword2.DATA}}=显示订单号

                订单金额：{{keyword3.DATA}}=显示订单金额

                商品信息：{{keyword4.DATA}}=显示购买的商品（有多种）

                {{remark.DATA}}=更多信息请到后台查看

             * */

            $message_data = [

                $AppidData['nick_name'],

                $membership_card_id,

                sprintf("%.2f",$need_pay),

                $membership_card_name

            ];

            $tem = [];

            $tem['first'] = '你的店铺有会员在线支付了一笔新的订单';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '更多信息请到后台查看';

            return $this->staff_id($AppidData['shop_id'],$template_id,$tem,$url);

        }



    }



    // 会员到店提醒

    public function member_store_reminder(){



    }



    // 会员通知 ---------------------------------------------------------------------------------------------------------

    /**

     * 预约成功提醒

     * parameter -- $member_id(Int) -------- 用户ID

     * parameter -- $nick_name(String) ----- 用户名称

     * parameter -- $mobile(Int) ----------- 用户手机号

     * parameter -- $abbreviation(String) -- 课程名称

     * parameter -- $day(String) ----------- 日期

     * parameter -- $start_time(String) ---- 时间

     */

    public function appointment_success_reminder($member_id,$nick_name,$mobile,$abbreviation,$day,$start_time){



        $AppidData = $this->member_info($member_id);

        if($AppidData['appointment_success_reminder'] && $AppidData['mp_openid']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['appointment_success_reminder'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，您已预约成功

                服务名称：{{keyword1.DATA}}=显示预约的课程名称

                预约日期：{{keyword2.DATA}}=显示预约的上课时间

                预约人：{{keyword3.DATA}}=显示预约的用户名称

                联系电话：{{keyword4.DATA}}=显示预约的用户手机号

                预约机构：{{keyword5.DATA}}=显示店铺名称

                {{remark.DATA}}=请提前安排好时间上课

             * */

            $message_data = [

                $abbreviation,

                $day.' '.$start_time,

                $nick_name,

                $mobile,

                $AppidData['name']

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，您已预约成功';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '请提前安排好时间上课';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }

    }



    // 服务完成提醒 - 未完成

    public function service_completion_reminder(){



//        $AppidData = $this->member_info($member_id);

//        $Openplatform = new OpenPlatformMiniappHelper();

//        $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

//        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

//        $template_id = $AppidData['member_appointment'];

//        // 构建请求

//        /*

//            {{first.DATA}}=尊敬的会员，您已预约成功

//            服务名称：{{keyword1.DATA}}=显示预约的课程名称

//            预约日期：{{keyword2.DATA}}=显示预约的上课时间

//            预约人：{{keyword3.DATA}}=显示预约的用户名称

//            联系电话：{{keyword4.DATA}}=显示预约的用户手机号

//            预约机构：{{keyword5.DATA}}=显示店铺名称

//            {{remark.DATA}}=请提前安排好时间上课

//         * */

//        $message_data = [

//            $abbreviation,

//            $day.' '.$start_time,

//            $nick_name,

//            $mobile,

//            $AppidData['name']

//        ];

//        $tem = [];

//        $tem['first'] = '尊敬的会员，您已预约成功';

//        foreach ($message_data as $key => $item) {

//            $tem['keyword' . ($key + 1)] = $item;

//        }

//        $tem['remark'] = '请提前安排好时间上课';

//        return $this->member_id($member_id,$template_id,$tem,$url);



    }



    /**

     * 预约取消提醒

     * parameter -- $member_id(Int) ---------------- 用户ID

     * parameter -- $membership_card_id(Int) ------- 开通ID

     * parameter -- $membership_card_name(String) -- 课程包或会籍卡的名称

     */

    public function appointment_cancellation_reminder($member_id,$abbreviation,$day,$start_time,$type){



        $AppidData = $this->member_info($member_id);

        if($AppidData['appointment_cancellation_reminder'] && $AppidData['mp_openid']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['appointment_cancellation_reminder'];

            // 构建请求

            /*

                {{first.DATA}}=您好，您的课程预约已取消

                课程类型：{{keyword1.DATA}}=显示预约的课程名称

                课程时间：{{keyword2.DATA}}=显示预约的上课时间

                取消来源：{{keyword3.DATA}}=会员自主取消预约

                {{remark.DATA}}=感谢您使用，欢迎再次光临

             * */

            $message_data = [

                $abbreviation,

                $day.' '.$start_time,

                $type

            ];

            $tem = [];

            $tem['first'] = '您好，您的课程预约已取消';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '感谢您使用，欢迎再次光临';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    /**

     * 会员卡开卡通知

     * parameter -- $member_id(Int) ---------------- 用户ID

     * parameter -- $membership_card_id(Int) ------- 开通ID

     * parameter -- $membership_card_name(String) -- 课程包或会籍卡的名称

     */

    public function membership_card_notice($member_id,$membership_card_id,$membership_card_name){



        $AppidData = $this->member_info($member_id);



        if($AppidData['membership_card_notice'] && $AppidData['mp_openid']){



            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['membership_card_notice'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，您已在本店开通XXX成功

                会员卡ID：{{keyword1.DATA}}=显示开通ID

                所属卡种：{{keyword2.DATA}}=显示开通的XXX名称

                开卡时间：{{keyword3.DATA}}=显示开通的时间

                {{remark.DATA}}=感谢您的开通

             * */

            $message_data = [

                $membership_card_id,

                $membership_card_name,

                date("Y-m-d H:i:s",time())

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，您已在本店开通'.$membership_card_name.'成功';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '感谢您的开通';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    /**

     * 核销通知

     * parameter -- $member_id(Int) --------- 用户ID

     * parameter -- $num(Int) --------------- 划扣次数

     * parameter -- $abbreviation(String) --- 课程包或会籍卡的名称

     * parameter -- $abbreviation_id(Int) --- 课程包或会籍卡的ID

     * parameter -- $abbreviation_num(Int) -- 核销后的剩余次数

     * parameter -- $abbreviation_day(Int) -- 距离到期的剩余天数

     */

    public function write_off_notice($member_id,$num,$abbreviation,$abbreviation_id,$abbreviation_num,$abbreviation_day){



        $AppidData = $this->member_info($member_id);

        if($AppidData['write_off_notice'] && $AppidData['mp_openid']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['write_off_notice'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，本次消费划扣X次

                卡种名称：{{keyword1.DATA}}=显示课程包或会籍卡的名称

                会员卡ID：{{keyword2.DATA}}=显示课程包或会籍卡的ID

                剩余可用次数：{{keyword3.DATA}}=显示核销后的剩余次数

                剩余可用天数：{{keyword4.DATA}}=显示距离到期的剩余天数

                发卡单位：{{keyword5.DATA}}=显示店铺名称

                {{remark.DATA}}=感谢您的光顾，欢迎再次光临

             * */

            $message_data = [

                $abbreviation,

                $abbreviation_id,

                $abbreviation_num,

                ($abbreviation_day/86400).'天',

                $AppidData['name'],

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，本次消费划扣'.$num.'次';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '感谢您的光顾，欢迎再次光临';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    /**

     * 充值成功通知

     * parameter -- $member_id(Int) -- 用户ID

     * parameter -- $pay(Int) -------- 充值金额

     * parameter -- $pay_give(Int) --- 赠送金额

     */

    public function recharge_success_notification($member_id,$pay,$pay_give = 0.0){



        $AppidData = $this->member_info($member_id);

        if($AppidData['recharge_success_notification']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['recharge_success_notification'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，您已充值成功

                充值时间：{{keyword1.DATA}}=显示充值的时间

                充值金额：{{keyword2.DATA}}=显示充值的金额

                赠送金额：{{keyword3.DATA}}=显示赠送的金额，无赠送显示0.0

                店铺名称：{{keyword4.DATA}}=显示店铺的名称

                {{remark.DATA}}=感谢您的支持

             * */

            $message_data = [

                date("Y-m-d H:i:s",time()),

                sprintf("%.2f",$pay),

                $pay_give,

                $AppidData['name'],

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，您已充值成功';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '感谢您的支持';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    /**

     * 扣除余额通知

     * parameter -- $member_id(Int) ---- 用户ID

     * parameter -- $de_balance(Int) --- 扣除余额

     * parameter -- $pay(Int) ---------- 扣除的金额

     * parameter -- $pay_balance(Int) -- 扣除后的剩余金额

     */

    public function notice_deduction_balance($member_id,$original_deduction,$remain_balance){



        $AppidData = $this->member_info($member_id);

        if($AppidData['notice_deduction_balance']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['notice_deduction_balance'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，您已成功使用余额消费

                交易类型：{{keyword1.DATA}}=扣除余额

                交易金额：{{keyword2.DATA}}=显示扣除的金额

                交易时间：{{keyword3.DATA}}=显示扣除的时间

                账户余额：{{keyword4.DATA}}=显示扣除后的剩余金额

                {{remark.DATA}}=感谢您的光顾，欢迎再次光临

             * */

            $message_data = [

                '扣除余额',

                $original_deduction,

                date("Y-m-d H:i:s",time()),

                sprintf("%.2f",$remain_balance),

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，您已成功使用余额消费';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '感谢您的光顾，欢迎再次光临';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    // 服务到期提醒 - 未完成

    public function service_expiration_reminder(){



//        $AppidData = $this->member_info($member_id);

//        $Openplatform = new OpenPlatformMiniappHelper();

//        $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

//        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

//        $template_id = $AppidData['member_appointment'];

//        // 构建请求

//        /*

//            {{first.DATA}}=尊敬的会员，您的XX还有XX天即将到期

//            业务号码：{{keyword1.DATA}}=显示课程包或会籍卡的ID

//            业务类型：{{keyword2.DATA}}=显示课程包或会籍卡的名称

//            到期时间：{{keyword3.DATA}}=显示课程包或会籍卡的到期时间

//            {{remark.DATA}}=为了不影响您的使用，请及时续费

//         * */

//        $message_data = [

//            $abbreviation,

//            $day.' '.$start_time,

//        ];

//        $tem = [];

//        $tem['first'] = '尊敬的会员，您的'..'还有'..'天即将到期';

//        foreach ($message_data as $key => $item) {

//            $tem['keyword' . ($key + 1)] = $item;

//        }

//        $tem['remark'] = '为了不影响您的使用，请及时续费';

//        return $this->member_id($member_id,$template_id,$tem,$url);



    }



    /**

     * 拼团成功提醒

     * parameter -- $member_id(Int) ------ 用户ID

     * parameter -- $goods_name(String) -- 商品名称

     * parameter -- $pay(Int) ------------ 订单金额

     */

    public function group_success_reminder($member_id,$goods_name,$pay){



        $AppidData = $this->member_info($member_id);

        if($AppidData['group_success_reminder'] && $AppidData['mp_openid']){

            $Openplatform = new OpenPlatformMiniappHelper();

            $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

            $template_id = $AppidData['group_success_reminder'];

            // 构建请求

            /*

                {{first.DATA}}=尊敬的会员，您已拼团成功

                拼团详情：{{keyword1.DATA}}=显示拼团的商品名称

                参团金额：{{keyword2.DATA}}=显示拼团的订单金额

                {{remark.DATA}}=请前往我的拼团中查看详情

             * */

            $message_data = [

                $goods_name,

                $pay

            ];

            $tem = [];

            $tem['first'] = '尊敬的会员，您已拼团成功';

            foreach ($message_data as $key => $item) {

                $tem['keyword' . ($key + 1)] = $item;

            }

            $tem['remark'] = '请前往我的拼团中查看详情';

            return $this->member_id($member_id,$template_id,$tem,$url);

        }



    }



    /**

     * 发货提醒

     * parameter -- $member_id(Int) -- 用户ID

     * parameter -- $sn(String) ------ 订单的编号

     * parameter -- $time(String) ---- 购买订单的时间

     * parameter -- $name(String) ---- 收货人姓名

     * parameter -- $mobile(Int) ----- 收货人电话

     */

    public function shipping_reminder($member_id,$sn,$time,$name,$mobile){



        $AppidData = $this->member_info($member_id);

        $Openplatform = new OpenPlatformMiniappHelper();

        $mp_token = $Openplatform->setShopId($this->request->shopId)->mp_refresh_token();

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$mp_token['authorizer_access_token'];

        $template_id = $AppidData['member_appointment'];

        // 构建请求

        /*

            {{first.DATA}}=您的订单已发货，正在配送中，请耐心等待。

            订单编号：{{keyword1.DATA}}=显示订单的编号

            购买时间：{{keyword2.DATA}}=显示购买订单的时间

            收货人：{{keyword3.DATA}}=显示收货人姓名

            收货电话：{{keyword4.DATA}}=显示收货人电话

            {{remark.DATA}}=如有问题，可联系商家

         * */

        $message_data = [

            $sn,

            $time,

            $name,

            $mobile

        ];

        $tem = [];

        $tem['first'] = '您的订单已发货，正在配送中，请耐心等待。';

        foreach ($message_data as $key => $item) {

            $tem['keyword' . ($key + 1)] = $item;

        }

        $tem['remark'] = '如有问题，可联系商家';

        return $this->member_id($member_id,$template_id,$tem,$url);



    }



    /**

     * 获取用户对应商家信息

     * parameter -- $member_id(Int) -- 用户ID

     */

    public function member_info($member_id){



        return Db::name('yoga_member')

            ->alias('ym')

            ->field('s.*,ym.*')

            ->where('ym.member_id',$member_id)

            ->join('shop s','ym.shop_id = s.shop_id')

            ->find();



    }



    /**

     * 获取员工id 并推送

     * parameter -- $shop_id(Int) --------- 商家ID

     * parameter -- $template_id(String) -- 模板ID

     * parameter -- $tem(Array) ----------- 内容

     * parameter -- $url(String) ---------- 地址

     */

    public function staff_id($shop_id,$template_id,$tem,$url){



        $error = '';

        $StaffId = Db::name('yoga_shop_staff')

            ->alias('yss')

            ->field('ym.mp_openid')

            ->where('yss.shop_id',$shop_id)

            ->where(['yss.status'=>1,'yss.is_delete'=>0])

            ->join('yoga_member ym',"yss.mobile = ym.mobile and ym.shop_id=".$shop_id)

            ->select();

        if(!empty($StaffId)){

            foreach($StaffId as $k=>$v){

                if($v['mp_openid']){

                    $params = [

                        'touser' => $v['mp_openid'],

                        'template_id' => $template_id,

                        'data' => $this->createData($tem)

                    ];

                    $error = $this->post($url, json_encode($params));

                }

            }

        }

        return $error;



    }



    /**

     * 获取用户id 并推送

     * parameter -- $member_id(Int) ------- 用户ID

     * parameter -- $template_id(String) -- 模板ID

     * parameter -- $tem(Array) ----------- 内容

     * parameter -- $url(String) ---------- 地址

     */

    public function member_id($member_id,$template_id,$tem,$url){



        $error = '';

        $MemberId = Db::name('yoga_member')

            ->where('member_id',$member_id)

            ->value('mp_openid');

        if(!empty($MemberId)){

            if($MemberId){

                $params = [

                    'touser' => $MemberId,

                    'template_id' => $template_id,

                    'data' => $this->createData($tem)

                ];

                $error = $this->post($url, json_encode($params));

            }

        }

        return $error;



    }



    /**

     * 生成关键字数据

     * parameter -- $data(Array) -- 关键字

     */

    private function createData($data)

    {

        $params = [];

        foreach ($data as $key => $value) {

            $params[$key] = [

                'value' => $value,

                'color' => '#333333'

            ];

        }

        return $params;

    }



    /**

     * 获取access_token

     * parameter -- $storeShopData(Array) -- 商家信息

     */

    protected function getAccessToken($storeShopData)

    {

        $cacheKey =  'message_puch' . $storeShopData['app_id'] . '@access_token';

        if (!Cache::get($cacheKey)) {

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$storeShopData['app_id']}&secret={$storeShopData['app_secret']}";

            $result = $this->get($url);

            $result_array = json_decode($result,true);

            Cache::set($cacheKey, $result_array['access_token'], 6000);

        }

        return Cache::get($cacheKey);



    }



    /**

     * 获取access_token

     * parameter -- $url(String) ------ 请求地址

     * parameter -- $data(Json) ------- 请求数据

     * parameter -- $useCert(String) -- 证书

     * parameter -- $sslCert(String) -- 证书

     */

    protected function post($url, $data = [], $useCert = false, $sslCert = [])

    {

        $header = [

            'Content-type: application/json;'

        ];

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

        curl_setopt($curl, CURLOPT_HEADER, false);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($curl, CURLOPT_POST, TRUE);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        if ($useCert == true) {

            // 设置证书：cert 与 key 分别属于两个.pem文件

            curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');

            curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);

            curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');

            curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);

        }

        $result = curl_exec($curl);

        curl_close($curl);

        return $result;



    }



    /**

     * 获取access_token

     * parameter -- $url(String) -- 请求地址

     */

    protected function get($url)

    {

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HEADER, 0);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts

        $result = curl_exec($curl);

        curl_close($curl);

        return $result;

    }



}